package com.micai.springboot.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.micai.springboot.base.BaseController;
import com.micai.springboot.dto.sys.SysMsgSendDto;
import com.micai.springboot.service.SysMsgSendService;
import com.micai.springboot.util.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.Map;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/10/26 15:14
 * @Description: 消息发送表 控制器
 */
@Controller
@RequestMapping("/admin/msgSend")
public class AdminMsgSendController extends BaseController {

    @Autowired
    private SysMsgSendService msgSendService;

    @RequestMapping(value = "/list")
    public String list(@RequestParam(value="pageNum", defaultValue="1") int pageNum,
                       @RequestParam(value="pageSize", defaultValue="10") int pageSize,
                       @RequestParam(required = false) String status,
                       Model model) {
        Page<SysMsgSendDto> sysMsgSendDtos = msgSendService.findPage(pageNum, pageSize, status);
        if (sysMsgSendDtos == null) {
            sysMsgSendDtos = new Page<SysMsgSendDto>();
        }
        PageInfo<SysMsgSendDto> page = new PageInfo<SysMsgSendDto>(sysMsgSendDtos);
        model.addAttribute("page", page);
        model.addAttribute("status", status);
        return "admin/msgSend/msgSendList";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> save(SysMsgSendDto sysMsgSendDto) {
        Map<String, Object> retMap = null;
        try {
            sysMsgSendDto.setStatus("0");//草稿
            sysMsgSendDto.setCreateBy(getId());
            sysMsgSendDto.setCreator(getId());
            sysMsgSendDto.setModifier(getId());
            sysMsgSendDto.setCreateDate(new Timestamp(System.currentTimeMillis()));
            if (msgSendService.save(sysMsgSendDto) == 1) {
                retMap = MessageUtils.success();
                return retMap;
            }
        } catch (Exception e) {
            logger.error("保存草稿失败: {}", e.getMessage());
            retMap = MessageUtils.error();
        }
        return retMap;
    }
}
